package com.example.Coupons.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.Coupons.entity.CouPonState;
import com.example.Coupons.entity.Coupon;
import com.example.Coupons.mapper.CouponMapper;
import com.example.Coupons.service.impl.CouponService;
import com.example.Utility.Result;
import com.example.Utility.publicUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
@RestController
@RequestMapping("coupon")
public class CouponController {
    @Resource
    private CouponService couponService;

    @Autowired
    private CouponMapper couponMapper;


    /**
     * 添加优惠券
     *
     * @return
     */
    @PostMapping("/add")
    public Boolean add(@RequestBody Coupon coupon) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        // 当前时间
        String newTime = sdf.format(new Date());
        // 创建日历对象
        Calendar calendar = Calendar.getInstance();
//        获取90天后的日期代码
        calendar.add(Calendar.DATE, 90);
//        获取代码转换为格式 转换成字符串
        String str2 = sdf.format(calendar.getTime());
        coupon.setBeginTime(newTime);
        coupon.setOverTime(str2);
        return couponService.save(coupon);
    }

    // 使用了优惠券修改
    @PostMapping("/update")
    public Boolean update(@RequestBody Coupon coupon) throws Exception {
        UpdateWrapper<Coupon> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", coupon.getId());
        coupon.setOverTime(publicUtils.getTimes("."));
        return couponService.update(coupon, updateWrapper);
    }


    /**
     * 返回前端优惠券处理
     *
     * @param coupon
     * @return
     */

    @GetMapping("/compare")
    public Result<CouPonState> compare(String openid) {
        //    已使用的
        List<Coupon> alreadyUse = new ArrayList<>();
        //    未使用的
        List<Coupon> notUse = new ArrayList<>();
        //    已失效
        List<Coupon> loseEfficacy = new ArrayList<>();
        //        创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        // 比较时间
        Date currentTime = null;
        Date couponTime = null;
        try {
            currentTime = sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
//        构造条件
        // 创建对象
        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Coupon::getOpenid, openid);
        wrapper.select().orderByDesc(Coupon::getOverTime);
        // 获取openid的所有优惠券
        List<Coupon> couponList = couponMapper.selectList(wrapper);
        for (Coupon coupon : couponList) {
            try {
                couponTime = sdf.parse(coupon.getOverTime());
                if (couponTime.getTime() > currentTime.getTime()) {
                    if (coupon.getState() == 0) {
                        notUse.add(coupon);//    未使用的
                    }
                } else {
                    if (coupon.getState() != 1) {
                        loseEfficacy.add(coupon);
                    }
                    // 失效了但是状态没有改为2
                    if (coupon.getState() != 2 && coupon.getState() != 1) {
                        coupon.setState(2);
                    }
                }
                if (coupon.getState() == 1) {
                    alreadyUse.add(coupon);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        final boolean b = couponService.updateBatchById(couponList);
        if (!b) {
            Objects.requireNonNull(couponList, "coupon  update is fail");
        }
        // 返回修改的数据列表
        CouPonState returnCoupon = new CouPonState();
        returnCoupon.setLoseEfficacy(loseEfficacy);
        returnCoupon.setNotUse(notUse);
        returnCoupon.setAlreadyUse(alreadyUse);
        return Result.succeed(returnCoupon);
    }

    /**
     * 指定商品id的优惠券
     */
    @PostMapping("/getUidCoupon")
    public Result getUidCoupon(@RequestBody Coupon coupon) throws Exception {
        List<Coupon> couPonStateList = new ArrayList<>();
//        创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        String newTimeDim = sdf.format(new Date());
//        构造条件
        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper();
        wrapper
                .eq(Coupon::getOpenid, coupon.getOpenid())
                .eq(Coupon::getAssignId, coupon.getAssignId())
                .eq(Coupon::getState, 0)
                .ge(Coupon::getBeginTime, newTimeDim);
        List<Coupon> couponList = couponService.list(wrapper);
        return Result.succeed(couponList);
    }

}
